import dayjs, { Dayjs } from 'dayjs'
import { FormSchema } from '../Form'

export const dateOptions = [
  { label: '日', value: 'day', picker: 'date', isRange: false, format: 'YYYY-MM-DD' },
  { label: '近7天', value: 'seven', picker: 'date', isRange: true, format: 'YYYY-MM-DD' },
  { label: '近30天', value: 'thirty', picker: 'date', isRange: true, format: 'YYYY-MM-DD' },
  { label: '周', value: 'week', picker: 'week', isRange: false, format: 'GGGG-wo' },
  { label: '月', value: 'month', picker: 'month', isRange: false, format: 'YYYY-MM' },
  { label: '季', value: 'season', picker: 'date', isRange: true, format: 'YYYY-MM-DD' },
  { label: '半年', value: 'halfyear', picker: 'date', isRange: true, format: 'YYYY-MM-DD' },
  { label: '年', value: 'year', picker: 'year', isRange: false, format: 'YYYY' },
  { label: '历年', value: 'hisYear', picker: 'year', isRange: true, format: 'YYYY' },
  { label: '自定义', value: 'custom', picker: 'date', isRange: true, format: 'YYYY-MM-DD' },
]

const pickerObj = {
  day: 'day',
  seven: 'custom',
  thirty: 'custom',
  hisYear: 'hisYear',
  custom: 'custom',
  season: 'custom',
  week: 'week',
  year: 'year',
  halfyear: 'custom',
}

export const pickerType = (value) => {
  const buttonValue = (value || 'day') as keyof typeof pickerObj
  return pickerObj[buttonValue] || buttonValue
}

export const setDisabledDate = (current: Dayjs, maxDate) => {
  maxDate = maxDate || dayjs().endOf('day')
  return current && current > maxDate
}

export const getSearchFormSchema = ({ disabledOption = [''], chooseMaxDate = {} }) => {
  let filterOptions = dateOptions
  
  if (disabledOption.length > 0) {
    disabledOption.forEach((dItem) => {
      filterOptions = filterOptions.filter((item) => item.value !== dItem)
    })
  }
  let initData = filterOptions[0]
  // picker为周的情况下，defaultValue赋值失败，暂时绕过处理
  if (filterOptions[0].value === 'week') {
    initData = filterOptions[1]
  }

  return [
    {
      field: 'queryType',
      label: '时间粒度',

      component: 'Select',
      colProps: { span: 4 },
      defaultValue: initData.value,
      componentProps: ({ formModel, formActionType }) => {
        return {
          options: filterOptions,
          allowClear: false,
          onChange: (val) => {
            const { isRange, format, picker, value } = filterOptions.filter(
              (item) => item.value === val,
            )[0]
            const { updateSchema } = formActionType
            console.log(chooseMaxDate)
            const defaultMaxDate = chooseMaxDate[val as any] || dayjs()
            const commonProps = {
              picker,
              disabledDate: (current) => setDisabledDate(current, defaultMaxDate),
              valueFormat: format,
            }
            const defaultDate = defaultMaxDate.format(format)
            // 处理周、季、半年、年的默认值
            switch (value) {
              case 'week':
                break
              case 'seven':
                // 计算当前日期往前7天到昨天的时间点
                const sixDaysAgo = defaultMaxDate.subtract(7, 'days').format(format)
                const lastDayAgo = defaultMaxDate.subtract(1, 'days').format(format)
                formModel['[startDay,endDay]'] = [sixDaysAgo, lastDayAgo]
                break
              case 'thirty':
                // 计算当前日期往前30天的时间点
                const thirtyDaysAgo = defaultMaxDate.subtract(30, 'days').format(format)
                formModel['[startDay,endDay]'] = [thirtyDaysAgo, defaultDate]
                break
              case 'season':
                // 计算当前时间处于第几个季度
                const currentQuarter = defaultMaxDate.quarter()
                // 计算当前季度的开始时间
                const startOfQuarter = defaultMaxDate.startOf('quarter').format(format)
                // 计算当前季度的结束时间
                const endOfQuarter = defaultMaxDate.endOf('quarter').format(format)
                formModel['[startDay,endDay]'] = [startOfQuarter, endOfQuarter]
                break
              case 'halfyear':
                // 计算当前日期往前6个月的时间点
                const sixMonthAgo = defaultMaxDate.subtract(6, 'month').format(format)
                formModel['[startDay,endDay]'] = [sixMonthAgo, defaultDate]
                break
              default:
                formModel['[startDay,endDay]'] = [defaultDate, defaultDate]
                break
            }
            formModel.onlyStartDay = defaultDate
            updateSchema([
              {
                field: 'onlyStartDay',
                ifShow: !isRange,
                componentProps: {
                  ...commonProps,
                },
              },
              {
                field: '[startDay,endDay]',
                ifShow: isRange,
                componentProps: {
                  ...commonProps,
                },
              },
            ])
          },
        }
      },
    },
    {
      field: 'onlyStartDay',
      label: '开始时间',
      component: 'DatePicker',
      colProps: { span: 4 },
      defaultValue: chooseMaxDate[initData.value] || dayjs(),
      componentProps: {
        picker: pickerType(initData.picker),
        valueFormat: initData.format,
        disabledDate: (current) => setDisabledDate(current, chooseMaxDate[initData.value]),
      },
    },
    {
      field: '[startDay,endDay]',
      label: '开始时间',
      colProps: { span: 6 },
      component: 'RangePicker',
      ifShow: false,
    },
  ] as FormSchema[]
}
